﻿using System;
using PIP.Shared;
using System.ServiceModel;
using System.ServiceModel.Discovery;

namespace PIP.Service
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("{0} has started".With("Client"));
            StartService();
            Console.ReadKey();
        }

        private const string hostAddress = "net.tcp://localhost:8001/";

        static void StartService()
        {
            using (var host = new ServiceHost(typeof(ServiceWork)))
            {
                var binding = new NetTcpBinding();
                host.AddServiceEndpoint(typeof(IServiceWork), binding, hostAddress);

                AddDiscoveryBehavior(host);
                host.Open();

                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.ReadLine();

                host.Close();
            }
        }

        static void AddDiscoveryBehavior(ServiceHostBase host)
        {
            host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
            var discoveryBehavior = new ServiceDiscoveryBehavior();
            host.Description.Behaviors.Add(discoveryBehavior);
        }
    }
}